using System.Text.RegularExpressions;

namespace CSharpPro._1028;

public class UserService
{
    private static List<User> RegisteredUsers = new List<User>();
    public void Service()
    {
        while (true)
        {
            Console.WriteLine("-----------1.登录-----------");
            Console.WriteLine("-----------2.注册-----------");
            Console.WriteLine("-----------3.退出-----------");
            Console.WriteLine("请输入数字来表达您的需求：");
            string input = Console.ReadLine();
            if (!int.TryParse(input, out int choice))
            {
                Console.WriteLine("请输入有效数字！");
                continue;
            }

            switch (choice)
            {
                case 1:
                    Login();
                    break;
                case 2:
                    register();
                    break;
                case 3:
                    Console.WriteLine("程序退出");
                    return;
                default:
                    Console.WriteLine("输入错误，请重新输入：");
                    break;
            }

        }
    }
    
    public void register()
    {
        Console.WriteLine("===========用户注册===========");
        Console.WriteLine("请输入用户名（3-15位，仅含字母，数字，下划线）：");
        string userName = Console.ReadLine().Trim();
        if (string.IsNullOrWhiteSpace(userName))
        {
            Console.WriteLine("用户名不可为空！");
            return;
        }

        if (userName.Length < 3 || userName.Length > 15)
        {
            Console.WriteLine("用户名长度必须为3-15个字符！");
            return;
        }

        
        if (!Regex.IsMatch(userName, @"^[a-zA-Z0-9]+$"))
        {
            Console.WriteLine("用户名只能包含字母，数字，下划线！");
        }

        Console.WriteLine("请输入密码（6-20位，含至少1个大写，1个小写，1个数字）：");
        string password = Console.ReadLine();
        if (password.Length < 6 || password.Length > 20)
        {
            Console.WriteLine("密码必须6-20个字符！");
            return;
        }

        if (!Regex.IsMatch(password, @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$"))
        {
            Console.WriteLine("密码必须包含至少1个大写字母，1个小写字母和1个数字！");
            return;
        }

        Console.WriteLine("请确认密码：");
        string confirmPwd = Console.ReadLine();
        if(password!=confirmPwd)
        {
            Console.WriteLine("两次输入密码不一致");
            return;
        }
        
        RegisteredUsers.Add(new User(userName, password));
        Console.WriteLine("注册成功！");
    }

     public void Login()
    {
        Console.WriteLine("===========用户登录===========");
        Console.WriteLine("请输入用户名：");
        string userName = Console.ReadLine().Trim();
        Console.WriteLine("请输入密码：");
        string password = Console.ReadLine();
        
        User user = RegisteredUsers.Find(u => u.UserName.Equals(userName)&&u.Password.Equals(password));
        if (user != null)
        {
            Console.WriteLine("登录成功，进入系统首页！");
        }
        else
        {
            bool userNameExists=RegisteredUsers.Exists(u => u.UserName.Equals(userName));
            Console.WriteLine(userNameExists?"密码错误！":"用户不存在！");
        }
    }

}